<template>
  <div>
    <zh-form  ref="formInfo" :colmun="1" :rules="rules" :model="formInfo"  labelAlign="right" >

      <zh-card style="width:100%">
        <zh-row>
          <zh-input style="float: left"  type="text" :disabled="editAble" :span="22"  prop="g0502" v-model="formInfo.g0502" label="任务名称"
                    :labelCol="{span:3}" :wrapperCol="{span:21}"/>
        </zh-row>
        <zh-row >
          <zh-input :span="11" prop="g0503" :disabled="editAble" :disabledDate="onDisabledDate" @change="g0503Change"  type="date"
                    label="开始时间" v-model="formInfo.g0503" :labelCol="{span:6}" :wrapperCol="{span:18}"></zh-input>
          <zh-input :span="11" prop="g0504" :disabled="editAble" :disabledDate="onDisabledDate" @change="g0504Change" type="date" label="结束时间" v-model="formInfo.g0504"  :labelCol="{span:6}" :wrapperCol="{span:18}"
                    style="margin-left: 15px"></zh-input>
        </zh-row >
        <zh-row>
          <zh-input style="float: left" prop="c0303" :span="22" type="select" :defaultFields='{label:"dictValue",value:"dictKey",}'
                    :options="dictData.c0303" :disabled="editAble" label="资源表单" v-model="formInfo.c0303"
                    :labelCol="{span:3}" :wrapperCol="{span:9}" @change="c0303Change"></zh-input>
        </zh-row>
        <zh-row>
          <inspect-task-setting ref="inspectTaskSetting" :span="22" label="督察设置" :form-info="this.formInfo"
                                @inspectDataChange="getInspectChangeData"  prop="inspectTaskSetting" :labelCol="{ span:3 }"  :wrapperCol="{ span:21 }">
            
          </inspect-task-setting>
        </zh-row>
      </zh-card>

    </zh-form>
  </div>
</template>

<script>
import modal from 'ant-design-vue/es/modal'
import { getBatchDict} from "@/services/system/api"
import {inspectTaskAdd,inspectTasksAddAndOpen,getInspectTaskDel
} from '@/services/system/whdngxlApi'
import moment from "moment";
import InspectTaskSetting from "@/pages/system/form/InspectTaskSetting";
export default {
  components:{
    InspectTaskSetting,
  },
  props:{
    listQuery:{
      type:Function,
      default:null
    }
  },
  data() {
    return {
      formInfo:{
        g0501:'',
        g0502:'',
        g0503:'',
        g0504:null,
        c0303:'',
        resourcesData:[],//随机和手工生成的点位数据集合
        // alreadyInspectPoint:[],//已经存在的督察设置数据
      },
      inspectSetData: [],
      inspectSetDataDel:[],
      rules:{},
      dictData: {
        c0303:[],
      },
      visible: false,
      visible2: false,
      inspectSetTitle:'添加',
      editIndex: "",
      settingType:"",
      allPointCount:null, //总点位数量
      choosePointCount:null, //已选点位数量
      surplusPointCount:null, //剩余点位数量
      editAble:false,

    }
  },

  created() {
    const param = {}
    param.dictFields = 'c0303'
    this.$http.post(getBatchDict, param).then(res => {
      this.dictData = res.result
    })
  },
  methods: {
    initData(row){
      if(row){
        this.editAble = true
        const param = {}
        param.g0501 = row.g0501
        this.$http.post(getInspectTaskDel, param).then(res => {
          if(res.result){
            this.formInfo.g0501 = res.result.g0501
            this.formInfo.g0502 = res.result.g0502
            this.formInfo.g0503 = res.result.g0503
            this.formInfo.g0504 = res.result.g0504
            this.formInfo.c0303 = res.result.c0303
            this.inspectSetData = res.result.inspectSetData

            const data = {}
            data.inspectSetData = this.inspectSetData
            data.g0501 = row.g0501
            this.$refs.inspectTaskSetting.initData(data);
          }
        })
      }
    },
    //日期选择器禁用的日期
    onDisabledDate(current){
      const time = moment().endOf('day').subtract(1, 'days')
      return current < time
    },
    c0303Change(){
      this.inspectSetData = []
      if(this.formInfo.c0303){
        const params = Object.assign(this.formInfo)

        this.$refs.inspectTaskSetting.c0303Change(params);
      }
    },
    getInspectChangeData(inspectDataChange){
      this.inspectSetData = inspectDataChange.inspectSetDataChild
      this.choosePointCount = inspectDataChange.choosePointCountChild
      // this.formInfo.alreadyInspectPoint = this.inspectSetData

   /*   this.surplusPointCount = inspectDataChange.surplusPointCountChild
      this.inspectSetTitle = inspectDataChange.inspectSetTitleChild*/
    },
    taskSave(type){
      if(!this.formInfo.g0502){
        modal.warn({title: '提示', content: '任务名称不能为空'})
        return
      }
      if(!this.formInfo.c0303){
        modal.warn({title: '提示', content: '资源表单不能为空'})
        return
      }
      if(!this.formInfo.g0503){
        modal.warn({title: '提示', content: '开始时间不能为空'})
        return
      }
      if(!this.formInfo.g0504){
        modal.warn({title: '提示', content: '结束时间不能为空'})
        return
      }
      if(this.inspectSetData.length === 0){
        modal.warn({title: '提示', content: '巡查设置数据不能为空'})
        return
      }
      if(this.formInfo.g0503 && this.formInfo.g0504){
        let g0503 = this.formInfo.g0503.replaceAll('-','');
        let g0504 = this.formInfo.g0504.replaceAll('-','');
        if(g0503>g0504){
          this.formInfo.g0503 = ''
          this.formInfo.g0504 = ''
          modal.warn({title: '提示', content: '结束时间不能小于开始时间'})
        }
      }
      let postUrl = inspectTaskAdd
      if(type===2){ //保存并开启
        postUrl = inspectTasksAddAndOpen
      }
      this.$loading.show('正在处理数据,请等待...')
      const params = Object.assign(this.formInfo)
      params.inspectSetData = this.inspectSetData
      setTimeout(() => {
        this.$http.post(postUrl, params).then((res) => {
          this.$message.info('保存成功')
          this.$emit("handleCancel")
          if(this.listQuery){
            this.listQuery()
          }
          this.$loading.hide()
        }).catch((error) => {
          console.log(JSON.stringify(error))
          this.$loading.hide()
        }).finally(() =>{
        })
      }, 50)
    },
    handleCancel() {
      this.visible = false;
    },
    showOrHide() {
      this.visible = !this.visible;
    },
    handleCancel2() {
      this.visible2 = false;
    },
    showOrHide2() {
      this.visible2 = !this.visible2;
    },
    handleOk() {
      this.$nextTick(() => {
        this.$refs.inspectSetDetailsForm.confirm();
      });
    },
    g0503Change(){
      if(this.formInfo.g0503 && this.formInfo.g0504){
        let g0503 = this.formInfo.g0503.replaceAll('-','');
        let g0504 = this.formInfo.g0504.replaceAll('-','');
        if(g0503>g0504){
          this.formInfo.g0503 = ''
          this.formInfo.g0504 = ''
          modal.warn({title: '提示', content: '结束时间不能小于开始时间'})
        }
      }
    },
    g0504Change(){
      if(this.formInfo.g0503 && this.formInfo.g0504){
        let g0503 = this.formInfo.g0503.replaceAll('-','');
        let g0504 = this.formInfo.g0504.replaceAll('-','');
        if(g0503>g0504){
          this.formInfo.g0503 = ''
          this.formInfo.g0504 = ''
          modal.warn({title: '提示', content: '结束时间不能小于开始时间'})
        }
      }
    },
  }
}
</script>

<style scoped>

</style>
